[MUSIC] Welcome to Deploying Web Apps using Flask,
after watching this video, you will be able to describe
Flask features, install Flask, create and
deploy a Python web app using Flask. Flask is a micro framework for
creating web applications quickly and easily with Python, Flask supports CRUD. Which is nothing but
Create, Read, Update, and Delete requests by making Post, Put,
Get, Update, and Delete requests. Here is the basic structure
of a Flask application. Notice the logo of the Flask package. Let's look at how you can use Flask for
CRUD operations. Post and Put requests are used for
creating objects or data, for example, you can use Post and
Put requests to create a user. The difference between the two is that
Post creates the object or data on every request, while Put creates the object or
data only on the first request and continues to update the object or
data in consecutive requests. In most web applications,
you will see Post being used for creating objects or data. You can use the Get request
to read data from the server, you can use Update requests to
update the existing data or objects, you can use Delete requests to
delete the existing data or objects. Note, most web applications
tend to use Post for creating, updating, and deleting objects and
data, and Get for reading, another video explains the Post,
Put, and Get requests in detail. Next, let's look at how to create
a web application with Flask. The first step is to install
the Flask package using Pip, the standard package manager for Python. Like all other packages,
you can use the command Pip Install Flask, which will get the most
current version of Flask. The Flask package is installed and you
are all set to create the web application. Next, import the Flask package,
instantiate Flask class, create a web application, and
then run the application. For the purposes of demonstration,
let's look at the web application that returns Hello World
string as a response to a Get request. Step one, install Flask. Step two, import the Flask
module from the Flask package, to do this, type from flask import Flask. Step three, create an object of
the Flask class as a web application with the name My first web Application and
store it as app. For example,
app = Flask("My first Application"), most applications use the reference
name app for clarity purposes. However, app is just a reference name and
you can use any other name. Step four, define the root and the method that will be invoked
when this route is accessed. For example, enter @app.route('/'), notice in this example, neither Get nor
Post have been specified. When you do not specify a request,
by default, Get is requested. Therefore in the example, the endpoint will now be able to
serve Get requests to the root. Step five, hello is the method,
written as def hello () :, the hello method will be invoked
when the system accesses the API endpoint that was
defined in the previous step. The code to do this is app.run (debug). It takes no parameters and
returns the string, Hello World. In the code,
this is written as return ' Hello World!'. Step six, add a condition that web application should run only if_ _name _ _ attribute is set to_ _ main _ _, by default _ _ name _ _ is set to _ _ main _ _ unless it is explicitly changed. You can save and run this like
any other Python application. Type app.run ( debug=True ). To start the server in a development
environment, you need to save the code in a Python file and
run it like any other Python application. When the web application server starts,
it gives you the IP address and the port the application
can be accessed from. To check the end-points,
you can open the browser and connect to this endpoint you see. In the server output,
127.0.0.1 at port 5000 and see the string Hello World being returned
from the web server application. Templates are nothing but pre-created HTML pages that
are served from the Web application. They can be both static and dynamic, by
default, the Flask application looks for the templates in a directory called
templates under the root directory. If the templates need to use images,
styling sheets or JavaScript files which are stored in a
folder named static in the root directory, the static pages are rendered as is. The dynamic pages usually have information
that is filled in dynamically for each request, these pages are often
based on the values passed as arguments. The arguments can either be
passed through the URL or be passed as request parameters. Let's look at a sample Flask application. You start with importing Flask to
create web application request, to handle the incoming request and
render_template. For rendering static and
dynamic HTML pages, type from flask import Flask,
render_template, request, next instantiate Flask and
set the static folder. For example, app = Flask( "My first Application"). The default folder name is static, but
you can have the static contents in a directory with a different name
as long as you set it explicitly. Notice there are three endpoints
in this web application. The first is /sample,
this will render a static HTML page, the image in this HTML is sourced
from the static directory. The code to accomplish this is app.route ('/sample'), in the second line, type, def getSampleHtml ():. Note, there is no space
between getSampleHtml, in the third line, type, return render_template ('sample.html'). The next one is /user/username
where the username that you see within angle brackets is the parameter,
the code is written as @app.route( '/user/<username>',
methods=['GET']). If you see the method has been explicitly set to get, this is just to show how you can set the type of request. If nothing is specified,
it is considered a Get request. The page is rendered with
the parameter we passed in the URL. The last one is /user/username where the
username is passed as a request parameter. The page is rendered with the parameter
which was passed as a request. In this video you learn that
Flask is a micro framework for creating web applications and
supports CRUD. Install the Flask package using Pip to
create a web application using Flask, import Flask,
instantiate Flask, run the app, you can render both static and
dynamic templates with Flask. [MUSIC]